$(document).ready(function () {
	getAllOffenseAjax();
	$('#show_add_modal_button').click(showAddOffenseTypeModal);
	$('#show_edit_modal_button').click(showEditOffenseTypeModal);
	$('#show_delete_modal_button').click(showDeleteModal);
	$('#delete_button').click(deleteOffenseAjax);
	$('#reload_button').click(getAllOffenseAjax);
});

function addOffenseAjax() {
	var formData = new FormData(document.getElementById('add_offense_form'));
	$.ajax({
		url: '/api/offense/create/',
		type: 'POST',
		data: formData,
		cache: false,
		contentType: false,
		processData: false,
		success: function (response) {
			$('#add_offense_modal').modal('hide');
			getAllOffenseAjax();
		}
	});
}
function getSelectedId() {
	return $('input[name=tab1]:checked').val()
}
function showAddOffenseTypeModal() {

	document.getElementById('add_offense_form').reset();
	$('#add_offense_button').click(addOffenseAjax);
	$('#add_offense_modal').modal('show');
}
function showEditOffenseTypeModal() {
	if (typeof getSelectedId() === 'undefined') {
		alert("You must select row.");
		return;
	}
	getOffenseAjax(getSelectedId());
	$('#add_offense_button').click(editOffenseAjax);
	$('#add_offense_modal').modal('show');
}
function showDeleteModal() {
	if (typeof getSelectedId() === 'undefined') {
		alert("You must select row.");
		return;
	}
	$('#delete_modal').modal('show');
}
function getAllOffenseAjax() {
	$.ajax({
		url: "/api/offense/get/all/",
		type: 'POST',
		data: {
			csrfmiddlewaretoken: $.cookie('csrftoken') // just the token value
		},
		success: function (response) {
			$('#offense_table_body').html('');
			var offense = eval(response);
			for (i = 0; i < offense.length; i++) {
				$('#offense_table_body').append('<tr>' + '<td><input class="tabradio" type="radio" name="tab1" value="' + offense[i].id + '" /></td><td>' + offense[i].id + '</td>' + '<td>' + offense[i].name + '</td>' + '<td>' + offense[i].harmfulness + '</td>' + '<td>' + offense[i].overview + '</td>' + '</tr>');
			}
		}
	});
}
function getOffenseAjax(offneseId) {
	$.ajax({
		url: "/api/offense/get/",
		type: 'POST',
		data: {
			csrfmiddlewaretoken: $.cookie('csrftoken'), // just the token value
			id:offneseId
		},
		success: function (response) {
			var offense = JSON.parse(response);
			$('#id_name').val(offense.name);
			$('#id_harmfulness').val(offense.harmfulness);
			$('#id_overview').val(offense.overview);

		}
	});
}
function deleteOffenseAjax() {
	$.ajax({
		url: "/api/offense/delete/",
		type: 'POST',
		data: {
			csrfmiddlewaretoken: $.cookie('csrftoken'), // just the token value
			id: getSelectedId()
		},
		success: function (response) {
			$('#delete_modal').modal('hide');
			getAllOffenseAjax();
		}
	});
}
function editOffenseAjax() {
	var formData = $('#add_offense_form').serialize() + '&id=' + getSelectedId();
	$.ajax({
		url: '/api/offense/edit/',
		type: 'POST',
		data: formData,
		cache: false,
		contentType: false,
		processData: false,
		success: function (response) {
			$('#add_offense_modal').modal('hide');
			getAllOffenseAjax();
		}
	});
}
